package com.starhub.domain.ai.controller;

import com.starhub.common.bean.ResultResp;
import com.starhub.common.exception.AppException;
import com.starhub.engine.milvus.service.IMilvusService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/starhub/milvus")
public class MilvusAiController {

    private static final Logger log = LoggerFactory.getLogger(MilvusAiController.class);

    @Autowired
    private IMilvusService milvusService;

    @GetMapping("/create")
    public ResultResp<Boolean> create(@RequestParam(value = "mark") String mark, 
                                    @RequestParam(value = "collectionName") String collectionName, 
                                    @RequestParam(value = "desc") String desc) {
        try {
            this.milvusService.setMark(mark);
            this.milvusService.create(collectionName, desc);
            return ResultResp.success(this.milvusService.hasCollect(collectionName));
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return ResultResp.fail(e.getMessage(),e);
        }
    }

    @PostMapping("/insert")
    public ResultResp<Boolean> insert(@RequestParam(value = "mark") String mark,
                                    @RequestParam(value = "collectionName") String collectionName,
                                    @RequestBody List<Map<String, Object>> dataList) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.insert(collectionName, dataList));
        } catch (Exception e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return ResultResp.fail(e.getMessage(),e);
        }
    }

    @GetMapping("/search")
    public ResultResp<List<Map<String, Object>>> search(@RequestParam(value = "mark") String mark,
                                                      @RequestParam(value = "collectionName") String collectionName,
                                                      @RequestParam(value = "vector") String vector,
                                                      @RequestParam(value = "topK", defaultValue = "10") Integer topK) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.search(collectionName, vector, topK));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @DeleteMapping("/delete")
    public ResultResp<Boolean> delete(@RequestParam(value = "mark") String mark,
                                    @RequestParam(value = "collectionName") String collectionName,
                                    @RequestParam(value = "ids") List<String> ids) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.delete(collectionName, ids));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @PutMapping("/update")
    public ResultResp<Boolean> update(@RequestParam(value = "mark") String mark,
                                    @RequestParam(value = "collectionName") String collectionName,
                                    @RequestBody Map<String, Object> updateData) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.update(collectionName, updateData));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @GetMapping("/get")
    public ResultResp<List<Map<String, Object>>> get(@RequestParam(value = "mark") String mark,
                                                   @RequestParam(value = "collectionName") String collectionName,
                                                   @RequestParam(value = "ids") List<String> ids) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.get(collectionName, ids));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @GetMapping("/count")
    public ResultResp<Long> count(@RequestParam(value = "mark") String mark,
                                @RequestParam(value = "collectionName") String collectionName) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.count(collectionName));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @DeleteMapping("/drop")
    public ResultResp<Boolean> drop(@RequestParam(value = "mark") String mark,
                                  @RequestParam(value = "collectionName") String collectionName) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.drop(collectionName));
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }

    @GetMapping("/list")
    public ResultResp<List<String>> listCollections(@RequestParam(value = "mark") String mark) {
        try {
            this.milvusService.setMark(mark);
            return ResultResp.success(this.milvusService.listCollections());
        } catch (Exception e) {
            if (!(e instanceof AppException)) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
            return ResultResp.fail("异常：" + e.getMessage() + "," + e.getCause().getMessage());
        }
    }
}
